<?php

namespace app\admin\controller;

use app\admin\model\Member as Member_model;
use app\admin\model\MemberEarningDetail;
use app\api\service\BlockChain;
use app\common\CacheKey;
use think\Db;

class Member extends AdminBase
{
    public function getList()
    {
        $data = Member_model::getInstance()->getList();
        $address = '';
        foreach ($data['data'] as &$item) {
            $tribal_earnings = MemberEarningDetail::getInstance()->getSum(['pid' => $item->user_address], 'earning');
            $item->tribal_earnings= $tribal_earnings;
            $address .= $item->user_address . ",";
        }

        $address = trim($address, ',');
        $dins = (new BlockChain())->getDinOrINGold($address);

        $this->assign("data", $data['data']);
        $this->assign("page", $data['page']);
        $this->assign("dins", $dins);
        $this->assign("keywords", input('param.keywords', ''));
        $this->assign("range", input('param.range', ''));

        return $this->fetch();
    }

    /**
     * @Title: export
     * @Describe
     * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception
     * @author lijunwei
     * @Date 2022/1/6 14:32
     */
    public function export()
    {
        $lists = Member_model::getInstance()->exportExcel();
        $result = [];
        foreach ($lists as $key => $list) {
            $result[$key] = [
                'id' => $list['id'],
                'add_time' => $list['add_time'],
                'user_address' => $list['user_address'],
                'pid' => $list['pid']
            ];
        }

        $header = [
            'A' => [
                'title' => 'ID',
                'value' => 'id',
            ],
            'B' => [
                'title' => '加入时间',
                'value' => 'add_time',
            ],
            'C' => [
                'title' => '用户地址',
                'value' => 'user_address',
            ],
            'D' => [
                'title' => '上级用户地址',
                'value' => 'pid',
            ]
        ];
        $name = '地址明细';

        outExcel($result, $header, $name);
    }
}
